<template>
  <div>
    <div class="haeder">
      <div class="title">供应商管理</div>
      <el-row>
        <el-input @change="querySupplierList" v-model="form.supplierName" placeholder="供应商名称" style="width: 206px">
          <i slot="suffix" class="el-icon-search"></i>
        </el-input>
<!--        <div class="supplier-tip">供应商统一登录网址：https://scrm.guolian-life.com/h5/#verification</div>-->
      </el-row>
    </div>
    <div class="g-card">
      <div class="right-btn">
        <el-button type="primary" icon="el-icon-plus" size="mini" :disabled="!limit" @click="supplierVisible = true" v-hasPermi="['supplier:manage:add']">新增供应商
        </el-button>
      </div>
      <div style="padding: 0 20px 20px 20px">
        <el-table :data="tableList" style="width: 100%" height="520px">
          <el-table-column type="index" width="50" label="序号" align="center"></el-table-column>
          <el-table-column prop="supplierId" label="供应商ID" align="center"></el-table-column>
          <el-table-column prop="supplierName" label="供应商名称" align="center"></el-table-column>
          <el-table-column prop="supplierAdd" label="供应商地址" align="center"></el-table-column>
          <el-table-column prop="mobile" label="负责人手机号" align="center"></el-table-column>
          <el-table-column prop="createBy" label="创建人" align="center"></el-table-column>
          <el-table-column prop="state" label="状态" align="center">
            <template slot-scope="{ row }">
              <el-switch v-hasPermi="['supplier:manage:state']" :disabled="!limit" @change="changeStatus(row)" v-model="row.state" :active-value="'0'" :inactive-value="'1'"
                :active-text="row.state === '1' ? '停用' : '可用'">
              </el-switch>
            </template>
          </el-table-column>
          <el-table-column prop="createTime" label="创建时间" align="center"></el-table-column>
          <el-table-column fixed="right" label="操作" width="140" align="center">
            <template slot-scope="scope">
              <el-button v-hasPermi="['supplier:manage:edit']" type="text" :disabled="!limit" @click="editSupplier(scope.row)">编辑</el-button>
              <el-button type="text" v-hasPermi="['supplier:manage:remove']" :disabled="!limit" @click="deleteSupplier(scope.row)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" background layout="->,total,sizes,prev,pager,next" :page-sizes="[20, 30, 50]"
          :total="total" :current-page.sync="form.pageNum" :page-size.sync="form.pageSize" class="table-page">
        </el-pagination>
      </div>
    </div>
    <!-- 新增供应商 -->
    <el-dialog :title="`${supplierForm.id ? '编辑供应商' : '新增供应商'}`" :visible.sync="supplierVisible" @close="closeDialog" width="520px">
      <el-form :model="supplierForm" :rules="supplierRulers" ref="supplierForm">
        <el-form-item label="供应商名称" prop="supplierName" label-width="110px">
          <el-input v-model="supplierForm.supplierName" clearable></el-input>
        </el-form-item>
        <el-form-item label="供应商地址" prop="supplierName" label-width="110px">
          <el-input v-model="supplierForm.supplierAdd" clearable></el-input>
        </el-form-item>
        <el-form-item label="负责人手机号" prop="mobile" label-width="110px">
          <el-input v-model="supplierForm.mobile" clearable></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button type="primary" @click="addSupplier">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  querySupplierList,
  addSupplier,
  editSupplier,
  deleteSupplier,
  editSupplierStatus,
} from '../../../api/communityOperating/supplier.js'
import { getInfo } from '@/api/login'

export default {
  name: 'PersonalSop',
  data() {
    var checkMoblie = (rule, value, callback) => {
      var reg = /^[1][3,4,5,6,7,8,9][0-9]{9}$/
      if (!reg.test(this.supplierForm.mobile)) {
        callback(new Error('手机号码必须以13,14,15,16,17,18,19开头，请核实'))
      } else {
        callback()
      }
    }
    return {
      limit: true,
      supplierVisible: false,
      total: 0,
      tableList: [],
      form: {
        supplierName: '',
        pageNum: 1,
        pageSize: 20,
      },
      supplierForm: {
        supplierName: '',
        supplierAdd: '',
        mobile: '',
      },
      supplierRulers: {
        supplierName: [
          { required: true, message: '请输入供应商名称', trigger: 'blur' },
          { max: 32, message: '您录入字数超过32字', trigger: 'blur' },
        ],
        supplierAdd: [
          { required: true, message: '请输入供应商地址', trigger: 'blur' },
          { max: 200, message: '您录入字数超过200字', trigger: 'blur' },
        ],
        mobile: [
          { required: true, message: '请输入负责人手机号', trigger: 'blur' },
          {
            min: 11,
            max: 11,
            message: '手机号码位数需为11位',
            trigger: 'blur',
          },
          { validator: checkMoblie, trigger: 'blur' },
        ],
      },
    }
  },
  async created() {
    const res = await getInfo()
    this.limit = res.isSupervisor
    this.querySupplierList()
  },
  methods: {
    // 当前每页数量切换
    handleSizeChange(value) {
      this.form.pageNum = 1
      this.form.pageSize = value
      this.querySupplierList()
    },
    // 当前页切换
    handleCurrentChange(value) {
      this.form.pageNum = value
      this.querySupplierList()
    },
    // 查询
    async querySupplierList() {
      const res = await querySupplierList(this.form)
      if (res.code === 200) {
        this.total = res.total
        this.tableList = res.rows
      }
    },
    // 新增
    addSupplier() {
      this.$refs.supplierForm.validate(async (isok) => {
        let res = ''
        if (isok) {
          if (this.supplierForm.id) {
            res = await editSupplier(this.supplierForm)
          } else {
            res = await addSupplier(this.supplierForm)
          }
        }
        if (res.code === 200) {
          this.$message.success(
            `${this.supplierForm.id ? '编辑成功' : '新增成功'}`
          )
          this.supplierVisible = false
          this.$refs.supplierForm.resetFields()
          this.supplierForm = { supplierName: '', supplierAdd: '', mobile: '' }
          this.querySupplierList()
        }
      })
    },
    editSupplier(row) {
      this.supplierForm.supplierName = row.supplierName
      this.supplierForm.supplierAdd = row.supplierAdd
      this.supplierForm.mobile = row.mobile
      this.supplierForm.id = row.id
      this.supplierVisible = true
    },
    deleteSupplier(row) {
      this.$confirm('确认删除该供应商?', '删除确认', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
      }).then(async () => {
        const res = await deleteSupplier(row.id)
        if (res.code === 200) {
          this.$message.success(res.data)
          this.querySupplierList()
        }
      })
    },
    async changeStatus(row) {
      const state = row.state === '1' ? '0' : '1'
      console.log(row)
      const res = await editSupplierStatus({
        id: row.id,
        state: state,
      })
      if (res.code === 200) {
        this.$message.success(res.data)
        this.querySupplierList()
      }
    },
    closeDialog() {
      this.supplierVisible = false
      this.$refs.supplierForm.resetFields()
      this.supplierForm = { supplierName: '', supplierAdd: '', mobile: '' }
    },
    resetSupplier() {
      this.form.supplierName = ''
      this.querySupplierList()
    },
  },
}
</script>

<style lang="scss" scoped>
@import '../../../styles/base.scss';
.header-supplierName-select {
  margin-left: 15px;
}

::v-deep .el-table th.el-table__cell.is-leaf,
.el-table td.el-table__cell {
  border-bottom: none !important;
}

::v-deep .el-table .el-table__header-wrapper th {
  background-color: #f8f8f9 !important;
}
.haeder {
  padding: 16px 20px 20px;
  background: #ffffff;
  border-radius: 8px;
  width: 100%;
  .title {
    font-size: 20px;
    font-family: PingFangSC-Medium, PingFang SC;
    font-weight: 500;
    color: #2a2a2a;
    margin-bottom: 20px;
  }
  .el-button {
    width: 78px;
    height: 32px;
    border-radius: 4px;
  }
}
.supplier-tip {
  display: inline-block;
  color: #868686;
  margin-left: 10px;
}
</style>
